<?php
/**
 *
 * Class DisplayController - Controller pour les pages du front office
 * @author andrinople
 *
 */
Doo::loadControllerAt("FrbNewsletterClientController", "frbNewsletter");
class DisplayController extends DooController{

  /**
   *
   * Controller de la page d'accueil
   */
  public function index() {
    $data['baseurl'] = Doo::conf()->APP_URL;
    $data['title'] = "Page d'accueil du site l'Alliance";
    $data['titlePage'] = "Accueil";

    $this->view()->render('accueil', $data);
  }

  /**
   *
   * Controller de la page de menu
   */
  public function menu() {
    $data['baseurl'] = Doo::conf()->APP_URL;
    $data['title'] = "Page de menu du site l'Alliance";
    $data['titlePage'] = "Menu";

    $this->view()->render('menuPage', $data);
  }

  /**
   *
   * Controller de la page des événements
   */
  public function evenements() {
    $data['baseurl'] = Doo::conf()->APP_URL;
    $data['title'] = "Page des événements du site l'Alliance";
    $data['titlePage'] = "Événements";
    Doo::loadControllerAt('FrbEvenementClientController', 'frbEvenement');
    $eve = new FrbEvenementClientController();
    $data['liste'] = $eve->getListeEvenement();

    $this->view()->render('evenements', $data);
  }

  /**
   *
   * Controller de la page des photos
   */
  public function photos() {
    $data['baseurl'] = Doo::conf()->APP_URL;
    $data['title'] = "Page des photos du site l'Alliance";
    $data['titlePage'] = "Photos";

    $this->view()->render('photos', $data);
  }

  /**
   *
   * Controller de la page de contact
   */
  public function contact() {
    Doo::conf()->TEMPLATE_COMPILE_ALWAYS=false;
    $data['baseurl'] = Doo::conf()->APP_URL;
    $data['title'] = "Page de contact du site l'Alliance";
    $data['titlePage'] = "Contact";

    Doo::loadHelper('DooForm');

    $action = $data['baseurl'] . "contact";
    $form = new DooForm(array(
                    'method' => 'post',
                    'action' => $action,
                    'attributes' => array('class' => 'contact_form'),
                    'elements' => array(
                        'nom' => array('text', array(
                                'required' => true,
                                'label' => 'Nom:',
                                'attributes' => array("style" => 'border:1px solid #000;',
                                    'class' => 'validate[required,custom[onlyLetter],length[1,100]] text-input'),
                                'element-wrapper' => 'div',
                                'label-wrapper' => 'div',
                                'validators' => array(
    array('username', 1, 100)
    )
    )), 'prenom' => array('text', array(
                                'required' => true,
                                'label' => 'Prénom:',
                                'attributes' => array("style" => 'border:1px solid #000;',
                                    'class' => 'validate[required,custom[onlyLetter],length[1,100]] text-input'),
                                'element-wrapper' => 'div',
                                'label-wrapper' => 'div',
                                'validators' => array(
    array('username', 1, 100)
    )
    )),
                        'email' => array('text', array(
                                'required' => true,
                                'label' => 'E-mail:',
                                'attributes' => array("style" => 'border:1px solid #000;',
                                    'class' => 'validate[required,custom[email]] text-input'),
                                'element-wrapper' => 'div',
                                'label-wrapper' => 'div',
                                'validators' => array(array('required', 'Champ requis'),
    array('email', 'E-mail invalide'))
    )),
                        'message' => array('textarea', array(
                                'required' => true,
                                'label' => 'Message:',
                                'attributes' => array("style" => 'border:1px solid #000;',
                                    'id' => 'message-field',
                                    'class' => 'validate[required,length[6,300]] text-input'),
                                'element-wrapper' => 'div',
                                'label-wrapper' => 'div',
                                'validators' => array(array('required', 'Champ requis'))
    )),
                        'submit' => array('submit', array(
                                'label' => "Envoyer",
                                'attributes' => array('class' => 'bouton-field'),
                                'field-wrapper' => 'div'
                                ))
                                )
                                ));

                                if ($this->isPost()) {
                                  echo "ok";

                                  if ($form->isValid($_POST)) {
                                    Doo::loadControllerAt("FrbMessageClientController","frbMessage");
                                    $frb= new FrbMessageClientController();
                                    $frb->setSender($_POST["nom"]." ".$_POST["prenom"]);
                                    $frb->setMail($_POST["email"]);
                                    $frb->setContent($_POST["message"]);
                                    $frb->store();
                                  }

                                }

                                $this->view()->form = $form->render();

                                $this->view()->render('contact', $data);
  }

  /**
   *
   * Description here
   * @return
   */
  public function newsletterAdd() {
    $newsletter = new FrbNewsletterClientController();
    $newsletter->addAddress($_POST['newsletter']);
    echo "done";
  }

  /**
   *
   * Description here
   * @return
   */
  public function search() {
    $data['baseurl'] = Doo::conf()->APP_URL;
    $data['title'] = "Page des événements du site l'Alliance";
    $data['titlePage'] = "Recherche";
    Doo::loadControllerAt('FrbEvenementClientController', 'frbEvenement');
    $eve = new FrbEvenementClientController();
    $data['liste'] = $eve->search($_POST['recherche']);

    $this->view()->render('evenements', $data);
  }

  private function isPost() {
    if ($_SERVER['REQUEST_METHOD'] == "POST") return true;
    else return false;
  }



}

?>